package com.github.gaojh.server;

import com.github.gaojh.config.Environment;
import com.github.gaojh.server.handler.HttpServerHandler;
import com.github.gaojh.server.handler.WebSocketServerHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * <p>
 *
 * </p>
 *
 * @author gaojh
 * @since 2020/3/20
 */
public class FlyingServerInitializer extends ChannelInitializer<Channel> {
    @Override
    protected void initChannel(Channel channel) throws Exception {
        channel.pipeline()
                .addLast("decoder", new HttpRequestDecoder())
                .addLast("support-compressor", new HttpContentCompressor())
                .addLast("support-aggregator", new HttpObjectAggregator(1024 * 1024))
                .addLast("encoder", new HttpResponseEncoder())
                .addLast("chunk", new ChunkedWriteHandler());
        if (Environment.me.isEnableWebsocket()) {
            channel.pipeline().addLast("websocket-handler", new WebSocketServerHandler());
        }
        channel.pipeline().addLast("business-handler", new HttpServerHandler());
    }
}
